// 先插一条数据进去
PUT movie/_doc/285
{
    "id":285,
    "revenue":961000000,
    "runtime":169,
    "original_title":"Pirates of the Caribbean: At World's End",
    "genres":[
        "Adventure",
        "Fantasy",
        "Action"
    ],
    "budget":300000000,
    "status":"Released",
    "vote_count":4500,
    "spoken_languages":[
        "English"
    ],
    "overview":"Captain Barbossa, long believed to be dead, has come back to life and is headed to the edge of the Earth with Will Turner and Elizabeth Swann. But nothing is quite as it seems.",
    "popularity":139.082615,
    "title":"Pirates of the Caribbean: At World's End",
    "release_date":"2007-05-19",
    "production_companies":[
        "Walt Disney Pictures",
        "Jerry Bruckheimer Films",
        "Second Mate Productions"
    ],
    "production_countries":[
        "United States of America"
    ],
    "homepage":"http://disney.go.com/disneypictures/pirates/",
    "original_language":"en",
    "@timestamp":"2020-11-05T07:45:22.107Z",
    "tagline":"At the end of the world, the adventure begins.",
    "@version":"1",
    "keywords":[
        "ocean",
        "drug abuse",
        "exotic island",
        "east india trading company",
        "love of one's life",
        "traitor",
        "shipwreck",
        "strong woman",
        "ship",
        "alliance",
        "calypso",
        "afterlife",
        "fighter",
        "pirate",
        "swashbuckler",
        "aftercreditsstinger"
    ],
    "vote_average":6.9
}

// 查看自动生成的索引
GET /movie

// 根据需求修改索引

// 删除索引
DELETE /movie

// 写入修改的索引
PUT movie
{
  "mappings" : {
      "properties" : {
        "@timestamp" : {"type" : "date"},
        "@version" : {"type" : "keyword"},
        "budget" : {"type" : "long"},
        "genres" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 20}}},
        "homepage" : {"type" : "keyword"},
        "id" : {"type" : "long"},
        "keywords" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 200}}},
        "original_language" : {"type" : "keyword"},
        "original_title" : {"type" : "text"},
        "overview" : {"type" : "text"},
        "popularity" : {"type" : "float"},
        "production_companies" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 200}}},
        "production_countries" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 200}}},
        "revenue" : {"type" : "long"},
        "runtime" : {"type" : "short"}, // 时间不会太长, 16bit完全够用了
        "spoken_languages" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 20}}},
        "status" : {"type" : "keyword"},
        "tagline" : {"type" : "text"},
        "title" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},
        "vote_average" : {"type" : "float"},
        "vote_count" : {"type" : "integer"},
        "profit": {    "type": "float"  }, // 这个利润这个字段需要自己计算
        "release_dates" : {
            "properties": {
                "date" :  {"type" : "date"}, 
                "year": {"type" : "short"}, 
                "month":  {"type" : "byte"}, 
                "day":  {"type" : "byte"}
            }
        }
      }
    }
}

GET /movie

GET _cat/indices/movie

GET /movie/_count

GET /movie/_search
{
    "query": {
        "bool": {
            "must": {
                "exists": {
                    "field": "profit"
                }
            }
        }
    }
}

// 更新值
PUT _ingest/pipeline/profit
{
  "description": "Calculates the profit",
  "processors": [
    {
      "script": {
        "source": "(ctx.revenue > 0) && (ctx.budget > 0) && (ctx.profit = (ctx.revenue + ctx.budget))"
      }
    }
  ],
    "on_failure" : [
    {
      "set" : {
        "field" : "_index",
        "value" : "failed-{{_index}}"
      }
    }
  ]
}

POST /movie/_search
{
    "query": {
        "bool": {
            "must": {
                "exists": {
                    "field": "profit"
                }
            }
        }
    }
}

POST /movie/_search
{
    "query": {
        "bool": {
            "must": {
                "term": {
                    "revenue": 0
                }
            }
        }
    }
}

// version_conflict_engine_exception
POST /movie/_update_by_query?pipeline=profit&conflicts=proceed
{
    "seq_no_primary_term": true,
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "profit"
                }
            }
        }
    }
}

GET movie/_search
{
    "query": {
        "term": {
            "id": {
                "value": 13279
            }
        }
    }
}


GET /movie/_search
{
    "query": {
        "bool": {
            "must": [
                {"term": {"release_dates.year": 2004}}
            ]
        }

    }
}